﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace new_problem
{
    class IOUtils
    {
        public static void PrintGridToFile(Double[] x, Double[] y, Double[,] grid, String filename)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            using (StreamWriter outfile = new StreamWriter(filename+".txt"))
            {
                for(int i=0; i< x.Length; i++)
                    for (int j = 0; j < y.Length; j++)
                        outfile.Write(x[i]+" "+y[j]+" "+grid[i,j]+"\n");
            }    
        }

        public static void PrintDataToFile(String data, String filename)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            using (StreamWriter outfile = new StreamWriter(filename + ".txt"))
            {
                outfile.Write(data);
            }
        }

        public static void readDataFromFile(String filename, Double[] x, Double[] y, Double[,] z)
        {
            try
            {
                using (StreamReader infile = new StreamReader(filename))
                {
                    String line;
                    for (int i = 0; i < DataGen.N + 1; i++)
                        for (int j = 0; j < DataGen.N + 1; j++)
                            if ((line = infile.ReadLine()) != null)
                            {
                                string[] str = line.Split(new char[] { ' ' }, 3);
                                x[j] = Double.Parse(str[0], System.Globalization.NumberStyles.Float) + 26.0;
                                y[i] = Double.Parse(str[1], System.Globalization.NumberStyles.Float) + 26.0;
                                z[j, i] = Double.Parse(str[2], System.Globalization.NumberStyles.Float);
                            }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
}
